package xkp.lesson1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestPool {
    public static void main(String[] args) throws InterruptedException {
        //创建一个固定数量的线程池
        //ExecutorService pool = Executors.newFixedThreadPool(5);
        //ExecutorService pool = Executors.newSingleThreadExecutor();
        //newCachedThreadPool全是临时线程，默认缓存一分钟，如果一分钟之内继续有任务来，可以继续执行
        ExecutorService pool = Executors.newCachedThreadPool();

        for(int i = 1; i <= 10; i++){
            Thread.sleep(5);
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
      /*  try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        });*/
    }
}
